<template>
  <div class="category-container">
    <div class="sidebar-wrapper">
      <van-sidebar v-model="activeCategory" @change="onCategoryChange">
        <van-sidebar-item v-for="category in categoryList" :key="category.id" :title="category.name" />
      </van-sidebar>
    </div>

    <div class="category-content">
      <div class="category-banner" v-if="currentCategory">
        <div class="category-desc">{{ currentCategory.desc }}</div>
        <img :src="currentCategory.picUrl" class="category-image" />
      </div>

      <div class="subcategory-grid">
        <div class="subcategory-item" v-for="sub in currentSubCategory" :key="sub.id" @click="goToGoodsList(sub.id)">
          <img :src="sub.picUrl" class="subcategory-image" />
          <div class="subcategory-name">{{ sub.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getCategoryList, getCurrentCategory } from '@/api/goods'

const router = useRouter()
const activeCategory = ref(0)
const categoryList = ref([])
const currentCategory = ref(null)
const currentSubCategory = ref([])
//获取左列表
const fetchCategoryList = async () => {
  const res = await getCategoryList()
  categoryList.value = res.data.categoryList
  fetchCategoryData(categoryList.value[0].id)
}
//获取右小分类
const fetchCategoryData = async (id) => {
  const res = await getCurrentCategory({ id })
  currentCategory.value = res.data.currentCategory
  currentSubCategory.value = res.data.currentSubCategory
}
//传左列表id给获取小分类方法
const onCategoryChange = (index) => {
  const category = categoryList.value[index]
  fetchCategoryData(category.id)
}
//跳转分类页面
const goToGoodsList = (id) => {
  router.push({
    path: '/goods/list',
    query: { id }
  })
}

onMounted(() => {
  fetchCategoryList()
})
</script>

<style scoped>
.category-container {
  display: flex;
  min-height: 100vh;
  min-width:78vh;
  background-color: #f7f8fa;
}

.sidebar-wrapper {
  width: 90px;
  height: 100%;
  overflow-y: auto;
  background-color: #f7f8fa;
}

.van-sidebar {
  width: 100%;
}

.van-sidebar-item {
  padding: 20px 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60px;
  box-sizing: border-box;
  text-align: center;
  line-height: 1.4;
  position: relative;
}

.van-sidebar-item:hover {
  background-color: #f0f0f0;
}

.van-sidebar-item--select {
  color: #ee0a24;
  background-color: #fff;
  font-weight: bold;
  position: relative;
}

.van-sidebar-item--select::before {
  content: '';
  position: absolute;
  top: 10;
  width: 4px;
  background-color: #ee0a24;
  transform: translateY(-50%);
  height: 100%;
  border-radius: 0 3px 3px 0;
}

.category-content {
  flex: 1;
  padding: 10px;
  overflow-y: auto;
  background-color: #fff;
}

.category-banner {
  margin-bottom: 15px;
  padding: 15px 0;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.category-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
  text-align: center;
  padding: 0 10px;
}

.category-image {
  width: 800px;
  height: auto;
  max-height: 150px;
  object-fit: cover;
  display: block;
  border-radius: 4px;
  margin: 0px auto;
}

.subcategory-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  padding: 0 5px;
  background: #fff;
}

.subcategory-item {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  text-align: center;
  padding: 15px 5px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
}

.subcategory-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.subcategory-item::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 12px;
  border: 1px solid rgba(238, 10, 36, 0.1);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.subcategory-item:hover::after {
  opacity: 1;
}

.subcategory-image {
  width: 60px;
  height: 60px;
  object-fit: contain;
  margin: 0 auto;
  transition: transform 0.3s ease;
}

.subcategory-item:hover .subcategory-image {
  transform: scale(1.1);
}

.subcategory-name {
  font-size: 12px;
  color: #333;
  margin-top: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.3s ease;
}

.subcategory-item:hover .subcategory-name {
  color: #ee0a24;
}
</style>